package cn.edu.hhu.teacher.service;

import cn.edu.hhu.teacher.bean.Course;
import cn.edu.hhu.teacher.bean.Teacher;
import cn.edu.hhu.teacher.dao.CourseDao;
import cn.edu.hhu.teacher.dao.TeacherDao;

import java.util.List;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();
    private CourseDao courseDao = new CourseDao();

    public Teacher login(Teacher teacher) {
        return teacherDao.queryTeacherByIdAndPassword(teacher.getTid(), teacher.getPwd());
    }

    public Teacher signup(Teacher teacher) {
        Teacher signUpTeacher = teacherDao.queryTeacherByIdAndName(teacher.getTid(), teacher.getTname());
        if (signUpTeacher == null) {
            return null;
        }
        if (signUpTeacher.isActivation()) {
            return teacher;
        }
        signUpTeacher.setPwd(teacher.getPwd());
        signUpTeacher.setActivation(true);
        teacherDao.updateTeacherById(signUpTeacher.getTid(), signUpTeacher.getPwd(), signUpTeacher.getDepartment(), signUpTeacher.isActivation());
        return signUpTeacher;
    }

    public boolean update(Teacher teacher) {
        return teacherDao.updateTeacherById(teacher.getTid(), teacher.getPwd(), teacher.getDepartment(), teacher.isActivation()) == 1;
    }

    public List<Course> queryCourses(Teacher teacher) {
        return courseDao.queryCoursesByTid(teacher.getTid());
    }
}
